package com.corx.network.core.model;

import com.corx.network.core.type.NetworkTypeEnum;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author gx
 * @version 1.0.0
 */
public class SendMessage extends MessageBase {

    /**
     * 发送方标识（设备 ID / 客户端 ID / topic / ip:port）
     */
    private String target;

    /**
     * 业务类型/逻辑路由
     */
    private String route;

    /**
     * 可选字段：是否需要响应
     */
    private boolean needReply;

    /**
     * 是否仅发送payload数据
     */
    private boolean onlyPayload;

    private SendMessage(Builder builder) {
        super(builder.deviceId, builder.messageId, builder.networkType, builder.payload, builder.headers);
        this.target = Objects.requireNonNull(builder.target, "target cannot be null");
        this.route = builder.route;
        this.needReply = builder.needReply;
        this.onlyPayload = builder.onlyPayload;
    }


    public static Builder builder(String target, byte[] payload) {
        return new Builder(target, payload);
    }

    public static class Builder {

        private String deviceId;
        private String messageId;
        private NetworkTypeEnum networkType;
        private final String target;
        private final byte[] payload;
        private Map<String, String> headers = new HashMap<>(1);
        private String route;
        private boolean needReply = false;
        private boolean onlyPayload = false;

        public Builder(String target, byte[] payload) {
            this.target = target;
            this.payload = payload;
        }

        public Builder networkType(NetworkTypeEnum networkType) {
            this.networkType = networkType;
            return this;
        }

        public Builder deviceId(String deviceId) {
            this.deviceId = deviceId;
            return this;
        }

        public Builder messageId(String messageId) {
            this.messageId = messageId;
            return this;
        }

        public Builder route(String route) {
            this.route = route;
            return this;
        }

        public Builder needReply(boolean needReply) {
            this.needReply = needReply;
            return this;
        }

        public Builder onlyPayload(boolean onlyPayload) {
            this.onlyPayload = onlyPayload;
            return this;
        }

        public Builder headers(Map<String, String> headers) {
            this.headers = headers;
            return this;
        }

        public SendMessage build() {
            return new SendMessage(this);
        }
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getRoute() {
        return route;
    }

    public void setRoute(String route) {
        this.route = route;
    }

    public boolean isNeedReply() {
        return needReply;
    }

    public void setNeedReply(boolean needReply) {
        this.needReply = needReply;
    }

    public boolean isOnlyPayload() {
        return onlyPayload;
    }

    public void setOnlyPayload(boolean onlyPayload) {
        this.onlyPayload = onlyPayload;
    }

    @Override
    public String toString() {
        return "SendMessage{" +
            "deviceId='" + deviceId + '\'' +
            ", networkType=" + networkType +
            ", messageId='" + messageId + '\'' +
            ", headers=" + headers +
            ", payload=" + Arrays.toString(payload) +
            ", target='" + target + '\'' +
            ", route='" + route + '\'' +
            ", needReply=" + needReply +
            ", onlyPayload=" + onlyPayload +
            '}';
    }
}
